﻿using _2112A_2_MES._XiangHe;
using _2112A_2_MES.XiangHes;
using _2112A_2_MES.XiangHes.Returns;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _2112A_2_MES.XiangHe.Returns
{
    [ApiExplorerSettings(GroupName = "项鹤")]
    public class ProduceSaleService:ApplicationService, IProduceSaleService
    {
        private readonly IRepository<SalesReturn, int> _returnrepository;
        public ProduceSaleService(IRepository<SalesReturn, int> returnrepository)
        {
            _returnrepository = returnrepository;
        }

        /// <summary>
        /// 获取退回订单列表
        /// </summary>
        /// <param name="rtCode"></param>
        /// <param name="rtName"></param>
        /// <param name="soCode"></param>
        /// <param name="clientName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/GetProReturnList")]
        public async Task<ReturnInfo<List<SalesReturn>>> GetProReturnList(string? rtCode, string? rtName, string? soCode, string? clientName, int pageIndex, int pageSize)
        {
            var returnList =await _returnrepository.GetListAsync();
            if (!string.IsNullOrEmpty(rtCode))
            {
                returnList = returnList.Where(x => x.ReturnCode.Contains(rtCode)).ToList();
            }
            if (!string.IsNullOrEmpty(rtName))
            {
                returnList = returnList.Where(x => x.ReturnName.Contains(rtName)).ToList();
            }
            if (!string.IsNullOrEmpty(soCode))
            {
                returnList = returnList.Where(x => x.PurchaseCode.Contains(soCode)).ToList();
            }
            if (!string.IsNullOrEmpty(clientName))
            {
                returnList = returnList.Where(x => x.VendorName.Contains(clientName)).ToList();
            }
            var totalCount = returnList.Count();
            returnList = returnList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (returnList != null)
            {
                return new ReturnInfo<List<SalesReturn>>(true,"0",returnList,totalCount);
            }
            return new ReturnInfo<List<SalesReturn>>(false,"1",null,0);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet, Route("/XiangHe/AddReturnOrder")]
        public async Task<int> AddReturnOrder(SalesReturn input)
        {
            input.CreateBy = "项鹤";
            input.CreateTime = DateTime.Now;
            await _returnrepository.InsertAsync(input);
            return 1;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet, Route("/XiangHe/DeletReturnOrder")]
        public async Task<int> DeletReturnOrder(int id)
        {
            await _returnrepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("/XiangHe/DeleteReturnRange")]
        public async Task<int> DeleteReturnRange(string id)
        {
            var array = id.Split(',');
            var list = await _returnrepository.GetListAsync();
            var input = list.Where(m => array.Contains(m.Id.ToString()));
            await _returnrepository.DeleteManyAsync(input);
            return 1;
        }

        /// <summary>
        /// 获取单个退回订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet, Route("/XiangHe/GetReturnOrder")]
        public async Task<SalesReturn> GetReturnOrder(int id)
        {
            var returnlist = await _returnrepository.GetAsync(id);
            return returnlist;
        }

        /// <summary>
        /// 更新退回订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet,Route("/XiangHe/UpdateReturnOrder")]
        public async Task<int> UpdateReturnOrder(SalesReturn input,int id)
        {
            var list = await _returnrepository.GetAsync(id);
            list.ReturnCode = input.ReturnCode;
            list.ReturnName = input.ReturnName;
            list.PurchaseCode = input.PurchaseCode;
            list.VendorName = input.VendorName;
            list.BatchCode = input.BatchCode;
            list.ReturnDate = input.ReturnDate;
            list.Status = input.Status;
            list.Remark = input.Remark;
            list.UpdateBy = "项鹤";
            list.UpdateTime = DateTime.Now;
            await _returnrepository.UpdateAsync(list);
            return 1;
        }
    }
}